package com.robotserver.orders.jobs.xxljob;

import com.robotserver.orders.service.ListenOrdersExpiredService;
import com.xxl.job.core.context.XxlJobHelper;
import com.xxl.job.core.handler.annotation.XxlJob;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
public class ListenOrdersExpiredSyncXxlJob {
    private static Logger logger = LoggerFactory.getLogger(ListenOrdersExpiredSyncXxlJob.class);

    @Autowired
    private ListenOrdersExpiredService listenOrderExpiredService;

    /**
     * 执行过期订单删除功能
     * 每天凌晨4点执行一次订单过期检查，删除10天前的订单（时间范围是10天)
     */
    @XxlJob("listenOrderExpiredHandler")
    public void listenOrderExpiredHandler() throws Exception {
        logger.info("--------ListenOrdersExpiredSyncXxlJob listenOrderExpiredHandler------监听token运行程序开始------");

        //默认持续时间为3天
        Integer duration = 259200;
        String durationStr = "";
        try {
            //接受参数-拉取当前日期往前的几秒订单
            durationStr = XxlJobHelper.getJobParam();
            if(StringUtils.isNotBlank(durationStr)){
                //从XXL-Job接受参数
                duration = Integer.parseInt(durationStr);
            }
        }catch (Exception e){
            logger.error("xxl-job para set is error {}:"+durationStr);
            //默认设置3天
            duration = 259200;
        }


        listenOrderExpiredService.listenOrdersExpired(duration);
    }

}
